package com.etc.team2.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.team2.service.LoginService;

public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/*获取login.jsp中的用户名和密码，调用LoginService中的LoginByNamePwd方法
		 * 若返回值是null，则输入有误，重新跳转登陆界面，若不为null，则登陆成功
		 * 将管理员信息保存到session中，跳转到管理页面
		 */
		request.setCharacterEncoding("utf-8");
		String managername=request.getParameter("username");
		String password=request.getParameter("password");
	    boolean result=new LoginService().LoginByNamePwd(managername, password);	    
	    if (result) {
//			Cookie cookie1=new Cookie("username", managername);
//		    Cookie cookie2=new Cookie("password", password);
//		    cookie1.setMaxAge(600);
//		    cookie2.setMaxAge(600);
//		    response.addCookie(cookie1);
//		    response.addCookie(cookie2);
			response.sendRedirect("manageframe.jsp");
		} else {
             System.out.println("登录失败");
             response.sendRedirect("home.jsp");
		}
		
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

}
